"use strict";
$(document).ready(function(){
	console.log('group_enroll loading....');
	

});

$(function() {
	$('#main_section_body').submit(registerGroup);
	$('#group_name').keyup(checkDuplicateGroupName);

});

function registerGroup() {	
	if(isPermitRegisterForm()) {
		new ajaxObject('../../group/addGroup.do', {	
			group_Name: $('#group_name').val(),
			group_Question : $('#group_question').val(),
			group_Inf: $('#group_intro').val()
			
		}, function(result) { 
			alert('그룹 생성에 성공하셨습니다. DB 등록 완료!');
		});
	}
	else {
		alert('가입폼을 제대로 입력해주세요.');
	}
	return false;
}

function isPermitRegisterForm() {

	if($('#group_name').val() === '' || $('#group_name').val() === null)
		return false;
	if($('#group_question').val() === '' || $('#group_question').val() === null)
		return false;
	if($('#group_intro').val() === '' || $('#group_intro').val() === null)
		return false;
	
	return true;
}

function checkDuplicateGroupName() {
	console.log('group_name duplicate check mode....');
	console.log($('#group_name').val());
	new ajaxObject('../../group/getGroup.do', {group_Name: $('#group_name').val()}, checkDuplicateGroupNameCallback);	
}

function checkDuplicateGroupNameCallback(result) {
//	console.log("result.user : " + result.user);
	if(result.group)
		$('#idcheck').html('중복된 그룹이름이 있습니다').css('color', 'red');
	else 
		$('#idcheck').html('사용할 수 있는 그룹이름입니다.').css('color', 'green');
}
